<?php
/**
 * Created by andrei
 * Date: 9/5/12
 * Time: 9:26 AM
 */
class Db_Cms_MenuPages extends Common {
	protected static $tableName = TAB_MENU_PAGES;
	protected static $dbFields = array(
		'id',
		'mp_menu_id',
		'mp_page_id',
		'mp_item_title',
		'mp_item_icon',
		'mp_parent_id',
		'mp_position',
		'mp_display',
		'deleted'
	);
	public static $positionField = 'mp_position';
    /**
     * @var Db_Cms_MenuPages
     */
    public $objectArray;

	public $id;
	public $mp_menu_id;
	public $mp_page_id;
	public $mp_item_title;
	public $mp_item_icon;
	public $mp_parent_id;
	public $mp_position;
	public $mp_display;
	public $deleted;

	function __construct($value = NULL, $fieldName = NULL) {
		parent::__construct($value, $fieldName);
	}

	/**
	 * @static
	 * @param $id
	 * @param $menuId
	 * @param $pageId
	 * @return bool //check if exist record with menuId and pageId
	 */
	public static function checkForDuplicate($id, $menuId, $pageId) {
		if (!$menuId || !$pageId) {
			return false;
		}
		$where = " `mp_menu_id` = ?d AND `mp_page_id` = ?d";
		if ($id) {
			$where .= " AND `id` != ".$id;
		}
		return self::$DB->selectCell("SELECT `id` FROM ?f WHERE {$where} AND `deleted` = 0", self::$tableName, $menuId, $pageId);

	}

	/**
	 * @static
	 * @param $parentId
	 * @param $menuId
	 * @return bool|mixed
	 * check if parent page id exist in this menu
	 */
	public static  function checkParentExistInMenu($parentId, $menuId) {
		if (!$parentId) {
			return true;
		}
		return self::$DB->selectCell("SELECT `id` FROM ?f WHERE mp_menu_id = ?d AND mp_page_id = ?d", self::$tableName, $menuId, $parentId);
	}

	/**
	 * @static
	 * @param $menuId
	 * @param bool $mpPageId
	 * @return bool|mixed
	 */
	public static function getPagesByMenuId($menuId, $mpPageId = false) {
		if (!$menuId) {
			return false;
		}
		$whereSql = '';
		if ($mpPageId) {
			$whereSql = ' AND `mp`.`mp_page_id` != '.$mpPageId;
		}
		return self::$DB->select("SELECT `mp`.`mp_page_id`,`p`.`p_name`,`p`.`p_title` FROM ?f `mp`
								INNER JOIN ?f `p` ON `mp`.`mp_page_id` = `p`.`id`
								INNER JOIN ?f `pt` ON `p`.`p_type_id` = `pt`.`id` AND `pt`.`deleted` = 0
									AND `pt`.`pt_title` LIKE 'menu_group'
								WHERE `mp`.`deleted` = 0 AND `p`.`deleted` = 0 AND `mp`.`mp_menu_id` = ?d " . $whereSql,
								self::$tableName, TAB_PAGES, TAB_PAGE_TYPES, $menuId);

	}

	/**
	 * @static
	 * @param $pageId
	 * @return array
	 */
	public static function getUsedMenusByPageId($pageId) {
		$sql = 'SELECT `mp_menu_id` FROM ?f WHERE `mp_page_id` = ? AND `deleted` = 0 ;';
		$pagesMenu = self::$DB->q($sql, self::$tableName, $pageId);
		$usedMenu = array();
		while($data = $pagesMenu->fetch_assoc()) {
			$usedMenu[] = $data['mp_menu_id'];
		}
		return $usedMenu;
	}

	public static function getProjectIdentifiersByPageId($pageId) {
		$sql ='SELECT `m`.`m_project_identifier` FROM ?f AS `mp`
				INNER JOIN ?f AS `m` ON `m`.`id` = `mp`.`mp_menu_id`
				WHERE `mp`.`mp_page_id` = ? AND `mp`.`deleted` = 0 AND `m`.`deleted` = 0
				GROUP BY `m`.`m_project_identifier` ';
		$projectIdentifiers = self::$DB->q($sql, self::$tableName, TAB_MENUS, $pageId);
		$identifiers = array();
		while($data = $projectIdentifiers->fetch_object()) {
			$identifiers[] = $data->m_project_identifier;
		}
		return $identifiers;
	}
}